package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.common.response.PageResult;
import com.itheima.common.response.ResponseResult;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：mengw9
 * @date ：Created By 2020/9/17 21:07
 * @description：
 * @version:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping()
    public List<User> findAll(){

        return this.userService.findAll();
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping()
    public ResponseResult<Void> addUser(@RequestBody User user){

        this.userService.addUser(user);
        return ResponseResult.okResult(201,"添加数据成功");
    }

    /**
     * 更新数据
     * @param user
     * @return
     */
    @PutMapping()
    public ResponseResult<Void> upadataUser(@RequestBody User user){

        this.userService.updateUser(user);
        return ResponseResult.okResult(200,"修改数据成功");

    }


    /**
     * 删除数据
     */
    @DeleteMapping()
    public ResponseResult<Void> deleteById(@RequestParam("id")Integer id){

        this.userService.deteUserById(id);

        return ResponseResult.okResult(200,"删除成功");
    }

    /**
     * 分页查询
     *
     */
    @GetMapping()
    public ResponseResult<User> queryUser(@RequestParam("page")Integer page, @RequestParam("size")Integer size){
        Page<User> users = this.userService.queryUser(page, size);
        return ResponseResult.okResult(users);

    }

}
